﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerQuanLyHopDong: IQuanLyHopDong
    {
        public QuanLyHopDong GetQuanLyHopDong(int oid)
        {
            string sql = @" Select NamHoc
                            From QuanLyHopDong
                            Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            QuanLyHopDong quanLyHopDong = new QuanLyHopDong();

            quanLyHopDong.Oid = oid;
            int id;
            if (int.TryParse(row["NamHoc"].ToString(), out id))
            {
                SqlServerNamHoc sqlServerNamHoc = new SqlServerNamHoc();
                quanLyHopDong.NamHoc = sqlServerNamHoc.GetNamHoc(id);
            }

            return quanLyHopDong;
        }

        public List<QuanLyHopDong> GetListQuanLyHopDong()
        {
            string sql = @" Select Oid, NamHoc
                            From QuanLyHopDong";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<QuanLyHopDong> list = new List<QuanLyHopDong>();

            foreach (DataRow row in dt.Rows)
            {
                QuanLyHopDong quanLyHopDong = new QuanLyHopDong();

                quanLyHopDong.Oid = int.Parse(row["Oid"].ToString());
                int id;
                if (int.TryParse(row["NamHoc"].ToString(), out id))
                {
                    SqlServerNamHoc sqlServerNamHoc = new SqlServerNamHoc();
                    quanLyHopDong.NamHoc = sqlServerNamHoc.GetNamHoc(id);
                }
               
                list.Add(quanLyHopDong);
            }
            return list;
        }

        public int Insert(QuanLyHopDong quanLyHopDong)
        {
            string sql = @" Insert into QuanLyHopDong(NamHoc)
                            Values(@NamHoc)";
            SqlParameter param = new SqlParameter("@NamHoc", quanLyHopDong.NamHoc.Oid);
            DataProvider.Insert(sql, param);
            return 0;
        }

        public int Update(QuanLyHopDong quanLyHopDong)
        {
            string sql = @" Update QuanLyHopDong
                            Set NamHoc = @NamHoc 
                             Where Oid = @Oid";
            SqlParameter[] param = {
                                       new SqlParameter("@Oid", quanLyHopDong.Oid),
                                       new SqlParameter("@NamHoc", quanLyHopDong.NamHoc.Oid)
                                    };
            return DataProvider.Update(sql, param);
        }

        public int Delete(QuanLyHopDong quanLyHopDong)
        {
            string sql = @" Delete From QuanLyHopDong Where Oid = @Oid";

            SqlParameter param = new SqlParameter("@Oid", quanLyHopDong.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(QuanLyHopDong obj)
        {
            throw new NotImplementedException();
        }
    }
}
